package com.stone;/*

 */

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * 创建索引
 * @ClassName EsTest_Client
 * @Author Final
 * @Project DataStructures
 */
public class EsTest_Doc_Query {

    public static void main(String[] args) throws IOException {

        //创建客户端
        RestHighLevelClient esClient=new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );

        //1、查询索引中的全部数据
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }


        //2、条件查询 termQuery
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",14)));
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }

        //3、分页查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        //（当前页码-1）*每页显示数据条数
////构造参数
//        //起始位置
//        builder.from(2);
//        //条数
//        builder.size(2);
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }

        //4、排序查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        builder.sort("age", SortOrder.ASC);
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }
        //5、过滤查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        String[] excludes={};
//        String[] includes={"name"};
//        builder.fetchSource(includes,excludes);
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }
        //6、组合查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        //构造条件
////        boolQueryBuilder.must(QueryBuilders.matchQuery("age",14));
////        boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","张三"));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age",14));
//        boolQueryBuilder.should(QueryBuilders.matchQuery("age",17));
//        builder.query(boolQueryBuilder);
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }

        //7、范围查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
//        rangeQueryBuilder.gte(10);
//        rangeQueryBuilder.lte(100);
//        builder.query(rangeQueryBuilder);
//
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }
        //8、模糊查询
//                SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//        FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "李").fuzziness(Fuzziness.AUTO);
//
//        builder.query(fuzziness);
//
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }
        //9、高亮查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "张三");
//
//        HighlightBuilder highlightBuilder=new HighlightBuilder();
//        highlightBuilder.preTags("<font color='red'>");
//        highlightBuilder.postTags("<font color='red'>");
//        highlightBuilder.field("name");
//
//        builder.highlighter(highlightBuilder);
//        builder.query(QueryBuilders.matchAllQuery());
//
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }

        //10、聚合查询
//        SearchRequest request=new SearchRequest();
//        request.indices("students");
//
//        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
//
//        AggregationBuilder builder1= AggregationBuilders.max("maxage").field("age");
//        builder.aggregation(builder1);
////        builder.query(QueryBuilders.matchAllQuery());
//
//        request.source(builder);
//
//        //查询数据
//        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);
//
//        SearchHits hits = search.getHits();
//        System.out.println(hits.getTotalHits());
//
//        for (SearchHit hit:hits){
//            System.out.println(hit.getSourceAsString());
//        }

        //11、分组查询
        SearchRequest request=new SearchRequest();
        request.indices("students");

        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());

        AggregationBuilder builder1= AggregationBuilders.terms("ageGroup").field("age");
        builder.aggregation(builder1);
//        builder.query(QueryBuilders.matchAllQuery());

        request.source(builder);

        //查询数据
        SearchResponse search = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();
        System.out.println(hits.getTotalHits());

        for (SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭es客户端
        esClient.close();
    }
}
